﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using DXApplicationTangche.UC.项目成果管理.在制品管理.service;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.生产计划.生产批生产.service;
using DXApplicationTangche.UC.项目成果管理.service;
using DXApplicationTangche.UC.门店出入库.Service;

namespace DXApplicationTangche.UC.项目成果管理.在制品管理
{
    public partial class FormVI质检QC : DevExpress.XtraEditors.XtraForm
    {
        private String code;    //  工序编码
        private String assertNo;    //  设备编码
        private String barcode { get; set; }//水洗标
        private String mnemonic { get; set; }//工号
        public 质检Model model;
        private OrderCommitDTO返回 orderCommitDTO返回 = new OrderCommitDTO返回();
        private QcBarcodeItemsDTO qcBarcodeItems = new QcBarcodeItemsDTO();
        public FormVI质检QC(String code, String assertNo)
        {
            InitializeComponent();
            this.code = code;
            this.assertNo = assertNo;
            this.ActiveControl = this.textBox水洗标;
        }

        private void textBox扫描水洗标_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.textBox水洗标.Text.Trim()))
                {
                    return;
                }
                this.barcode = this.textBox水洗标.Text.Trim();
                this.成衣码查询();
            }
        }

        private void 成衣码查询()
        {
            this.clearTextBoxs();
            this.label消息.Text = "消息：" + this.barcode;
            this.mnemonic = 生产批生产Service.get查找设备session对应的员工(assertNo);
            this.label当前操作人.Text = "当前操作人：" + this.mnemonic;
            //  数据查询显示
            this.refreshData();
            //  没扫工牌提示
            if (!String.IsNullOrEmpty(this.mnemonic))
            {
                this.textBox工号.BackColor = Color.White;
            }
            else
            {
                this.label消息.Text = "消息：请扫描您的工号!";
                this.ActiveControl = this.textBox工号;
                this.textBox工号.BackColor = Color.Red;
            }
            
        }

        private void refreshData()
        {
            try
            {
                this.qcBarcodeItems = 在制品管理Service.getQcItemsDTOByBarcode(this.barcode);
                this.orderCommitDTO返回 = 订单预览新Service.getOrderCommitDTOByBarcode(this.barcode);
            }
            catch
            {
                MessageBox.Show("无查询信息");
            }
            this.model = new 质检Model(qcBarcodeItems);
            this.显示();
            this.刷新显示半成品量尺();
            this.刷新显示半成品设计点();
        }

        private void 刷新显示半成品量尺()
        {
            //转换显示
            this.treeList半成品量尺.DataSource = this.model.list半成品量尺呈现;
            this.treeList半成品量尺.RefreshDataSource();
            this.treeList半成品量尺.ExpandAll();
        }

        private void 刷新显示半成品设计点()
        {
            //转换显示
            this.gridControl半成品设计点.DataSource = this.model.list半成品设计点呈现;
            this.gridView半成品设计点.RefreshData();
        }
        private void 显示()
        {
            this.richTextBox备注.Text = this.qcBarcodeItems.remarks;
            this.textBox款式.Text = this.qcBarcodeItems.styleNameCn;
            try { this.textBox尺码.Text = this.qcBarcodeItems.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { this.textBox尺码.Text = ""; }
            this.textBox面料名称.Text = this.qcBarcodeItems.materialNameCn;
            this.textBox面料编码.Text = this.qcBarcodeItems.materialCode;
            this.pictureBox款式.LoadAsync(URL.图片url + URL.款式图片路径 + this.qcBarcodeItems.styleImagePath);
            this.pictureBox面料.LoadAsync(URL.图片url + URL.面料图片路径 + this.qcBarcodeItems.materialImagePath);
            this.textBox订单时间.Text = Convert.ToDateTime(this.orderCommitDTO返回.orderDate).ToString();
            this.textBox客户名.Text = this.orderCommitDTO返回.customerName;
            this.textBox订单号.Text = this.orderCommitDTO返回.orderNo;
        }

        private void textBox工号_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.mnemonic))
                {
                    if (String.IsNullOrWhiteSpace(项目成果Service.get判断扫描员工码是否正确(this.textBox工号.Text.Trim())))
                    {
                        this.textBox工号.Clear();
                        MessageBox.Show("扫描员工码错误，请重新扫描");
                        this.textBox工号.BackColor = Color.Red;
                        return;
                    }
                    this.textBox工号.BackColor = Color.White;
                    this.mnemonic = this.textBox工号.Text.Trim();
                    生产批生产Service.save保存设备session(
                        this.assertNo,
                        this.mnemonic
                        );
                    this.label消息.Text = "消息：";
                    this.label当前操作人.Text = "当前操作人：" + this.mnemonic;
                }
                if (String.IsNullOrEmpty(this.barcode))
                {
                    MessageBox.Show("请扫描水洗标");
                    return;
                }
                this.clearTextBoxs();
                this.ActiveControl = this.textBox水洗标;
            }
        }

        /// <summary>
        /// 检查尺寸通过
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void repositoryItemCheckEdit1_Click(object sender, EventArgs e)
        {
            try
            {
                String id = this.treeList半成品量尺.FocusedNode["id"].ToString();
                //  尺寸通过
                在制品管理Service.inspectSizePassByBarcode(this.barcode,id);
                //  改变页面显示
                this.model.build半成品量尺通过(id);
                //  刷新
                //this.刷新显示半成品量尺();
                this.treeList半成品量尺.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void simpleButton尺寸全部通过_Click(object sender, EventArgs e)
        {
            try
            {
                //  尺寸全通过，并改变页面显示
                this.model.build半成品量尺通过All(this.barcode);
                //  刷新
                //this.刷新显示半成品量尺();
                this.treeList半成品量尺.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 检查设计点通过
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void repositoryItemButtonEdit设计点_Click(object sender, EventArgs e)
        {
            try
            {
                String itemCd =
                    FunctionHelper.GetgridViewValue(this.gridView半成品设计点, this.gridView半成品设计点.FocusedRowHandle, "itemCd");
                String itemValue =
                    FunctionHelper.GetgridViewValue(this.gridView半成品设计点, this.gridView半成品设计点.FocusedRowHandle, "itemValue");
                //  设计点通过
                在制品管理Service.inspectDesignItemPassByBarcode(
                        this.barcode,
                        this.model.build取单行设计点(itemCd, itemValue)
                    );
                //  改变页面显示
                this.model.build半成品设计点通过(itemCd, itemValue);
                //  刷新
                this.刷新显示半成品设计点();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void simpleButton设计点全部通过_Click(object sender, EventArgs e)
        {
            try
            {
                //  设计点全部通过,并改变页面显示
                this.model.build半成品设计点通过All(this.barcode);
                //  刷新
                this.刷新显示半成品设计点();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void treeList半成品量尺_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
        {
            if (e.Column.Name == "treeListColumnIfPass")
            {
                InspectSizeVO row = this.treeList半成品量尺.GetDataRecordByNode(e.Node) as InspectSizeVO;
                if (!row.ifPass)
                {
                    e.Appearance.BackColor = Color.Red;
                    e.Appearance.ForeColor = Color.White;
                }
                else if (row.ifPass)
                {
                    e.Appearance.BackColor = Color.Green;
                    e.Appearance.ForeColor = Color.White;
                }
            }
        }

        private void gridView半成品设计点_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name == "gridColumnifPass")
            {
                if (e.CellValue.Equals("未通过"))
                {
                    e.Appearance.BackColor = Color.Red;
                    e.Appearance.ForeColor = Color.White;
                }
                else if (e.CellValue.Equals("通过"))
                {
                    e.Appearance.BackColor = Color.Green;
                    e.Appearance.ForeColor = Color.White;
                }
            }
        }

        private void simpleButton提交_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.barcode)
                    || string.IsNullOrEmpty(this.code)
                    )
            {
                MessageBox.Show("无扫描内容");
                this.ActiveControl = this.textBox水洗标;
                return;
            }
            if (string.IsNullOrEmpty(this.mnemonic))
            {
                MessageBox.Show("未扫描工牌");
                this.ActiveControl = this.textBox工号;
                return;
            }
            //  提交
            try
            {
                String stepName = "";
                if ("01.0005".Equals(this.code))
                {
                    stepName = BarcodeWipInstanceConsts.HALF_PRODUCT_INSPECTION;
                }
                else if ("01.0005.01".Equals(this.code))
                {
                    stepName = BarcodeWipInstanceConsts.FIRST_PRODUCT_INSPECTION;
                }
                else if ("01.0007".Equals(this.code))
                {
                    stepName = BarcodeWipInstanceConsts.FINISHED_PRODUCT_INSPECTION;
                    //  成品检查入待检仓
                    出入库Service.garmentQCWaitStockIn(this.barcode);
                }
                if (!String.IsNullOrEmpty(this.barcode))
                {
                    //  生产工序执行
                    DXApplicationTangche.UC.下单MTO.Service.PlainMessageResult plainMessageResult = 
                        生产批生产Service.wipBarcodeScan(this.barcode, stepName, this.mnemonic);
                    //  成品检查不卡工序
                    if (!"01.0007".Equals(this.code))
                    {
                        if (!"200".Equals(plainMessageResult.errorCode))
                        {
                            MessageBox.Show(plainMessageResult.errorMessage);
                            return;
                        }
                        else
                        {
                            MessageBox.Show("操作完成");
                        }
                    }
                    else
                    {
                        MessageBox.Show("操作完成");
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
            this.textBox水洗标.Clear();
            this.textBox水洗标.Focus();
        }

        private void simpleButton清空水洗标_Click(object sender, EventArgs e)
        {
            this.textBox水洗标.Clear();
            this.ActiveControl = this.textBox水洗标;
        }
        private void clearTextBoxs()
        {
            this.textBox水洗标.Clear();
            this.textBox工号.Clear();
            this.label消息.Text = "消息：";
        }
        private void simpleButton退出当前操作人_Click(object sender, EventArgs e)
        {
            this.clearTextBoxs();
            this.ActiveControl = this.textBox水洗标;
            this.mnemonic = "";
            生产批生产Service.save保存设备session(
                    this.assertNo,
                    this.mnemonic
                    );
            this.label当前操作人.Text = "当前操作人：" + this.mnemonic;
        }
    }
}